package com.boolib.animate.framework.service.extend.impl;



import com.boolib.animate.commons.utils.UploadUtil;
import com.boolib.animate.framework.service.extend.IUploadService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 功能描述：
 *
 * @author Administrator
 * @description
 * @since 2019/12/27 15:30
 */
@Slf4j
@Service
public class UploadServiceImpl implements IUploadService {



	@Override
	public String uploadImage(String path, MultipartFile multipartFile) throws IOException, FileUploadException {

		return UploadUtil.transferTo(path,multipartFile);
	}

	@Override
	public List<String> uploadImages(String path, List<MultipartFile> multipartFiles) throws IOException,FileUploadException {

		List<String > list = new ArrayList();

		Iterator<MultipartFile> iterator = multipartFiles.iterator();

		while(iterator.hasNext()) {
			list.add(UploadUtil.transferTo(path,iterator.next()));
		}

		return list;
	}

	@Override
	public void removeOne(String path, String name) {
		File file = new File(path+File.separator+name);
		if(file.exists()){
			file.delete();
		}

	}

	@Override
	public void removeList(String path, List<String> names) {

		Iterator<String> iterator = names.iterator();

		while(iterator.hasNext()){
			File file = new File(path+File.separator+iterator.next());
			if(file.exists()){
				file.delete();
			}
		}


	}


}
